/*
 * Created on Nov 28, 2004
 */
package org.vectrics.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.vectrics.DataException;
import org.vectrics.provider.BeanLocator;

/**
 * @author mmoore
 */
public class HibernateExecute {
	protected final Log log = LogFactory.getLog(this.getClass());
	private HibernateUtil hibernate = null;
	public static final String BEAN_CODE = "hibernate.util";

	public HibernateExecute(HibernateUtil hibernate) {
		this.hibernate = hibernate; 
	}
	
	public HibernateExecute() {
	    hibernate = (HibernateUtil)BeanLocator.findBean(BEAN_CODE);
	}
	
	public void execute(HibernateCommand command) {
		Session session = null;
		int count = 0;
		try {
			session = hibernate.getThreadSession();
			command.execute(session);
		}
		catch (Exception ex) {
			log.error("Error doing hibernate task: " + this.getClass(), ex);
			throw new DataException(ex);
		}
		finally {
			if (session != null) {
				hibernate.closeSessionOld(session);
			}
		}
	}

	
}
